package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 公共服务事项清单
 */

public class GonGongFuWuList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {

        return null;
    }

    public JSONObject parseListPage(){
        //创建获取详情页信息的对象
        AllInfo allInfo = new AllInfo();
        //列表当前页
        int currentPage = 1;
        //列表总页数
        int pags = 0;
        //遍历列表每一页
        while (currentPage != pags) {
            String url = "https://wssp.hainan.gov.cn/wssp3/zwgk/checklist";
            JSONObject header = new JSONObject();
            JSONObject params = new JSONObject();
            params.put("dept", "");
            params.put("regionId", "HZ2881f4424539dd0142453c856b0025");
            params.put("taskType", "");
            params.put("isOnLine", "");
            params.put("taskName", "");
            params.put("pageNumber", currentPage);
            params.put("pageSize", "10");
            params.put("pdmsType", "1");
            params.put("qdCategory", "ggfwqd");
            params.put("cfg_dept_id", "");
            header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/govpublic_detail?qdCategory=bjmspqd");
            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            String listStrResponse = postFormReq(url, header, params);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
            JSONArray deptList = listResponse.getJSONObject("data").getJSONArray("list");
            if (deptList != null && deptList.size()>0) {
                for (Object o : deptList) {
                    JSONObject dept = (JSONObject) o;
                    JSONArray deptResultList = new JSONArray();
                    JSONObject result = new JSONObject();
                    JSONArray subdatas = dept.getJSONArray("subdatas");
                    for (Object o1 : subdatas) {
                        JSONObject subdata = (JSONObject) o1;
                        //向详情页信息对象传入请求参数
                        allInfo.setId(subdata.getString("id"));
                        allInfo.setSourcekey(subdata.getString("sourcekey"));
                        //获取封装好的详情页数据
                        allInfo.init();
                        try {
                            deptResultList.add(allInfo.getAllInfo());
                        } catch (Exception e) {
                            break;
                        }
                    }

                    result.put("名称", dept.getString("taskname"));
                    result.put("选项", deptResultList);
                    System.out.println(currentPage+"\n"+result);//测试用代码
                    return processResult(
                            null,
                            null,
                            null,
                            allInfo.getResponce(),
                            result,
                            "海南省",
                            "政务公开-公共服务清单"
                    );
                }
            }
            //更新当前页
            currentPage++;
            //更新总页数
            pags = listResponse.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
        }
        return null;
    }

    public static void main(String[] args) {
        GonGongFuWuList gonGongFuWuList = new GonGongFuWuList();
        gonGongFuWuList.parseListPage();
    }
}
